package com.cloudlead.shipownerAction.service.impl;

import com.cloudlead.common.lang.persistence.*;
import com.cloudlead.commonData.service.BaseCompanyOfficeService;
import com.cloudlead.commonData.service.TbContainerInfoService;
import com.cloudlead.commonData.service.TbCustomerOrderService;
import com.cloudlead.commonData.service.TbFleetOrderService;
import com.cloudlead.commonData.vo.*;
import com.cloudlead.shipownerAction.service.CustomerOrderSer;
import com.cloudlead.shipownerService.vo.CustomerOrderVo;
import com.cloudlead.shipownerService.vo.TbContainerInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/11/9.
 */
@Service
public class CustomerOrderImpl implements CustomerOrderSer {
    @Autowired
    TbCustomerOrderService tbCustomerOrderService;

    @Autowired
    TbContainerInfoService tbContainerInfoService;

    @Autowired
    TbFleetOrderService tbFleetOrderService;

    @Autowired
    BaseCompanyOfficeService baseCompanyOfficeService;

    @Override
    public Page<TbCustomerOrder> orderList(CustomerOrderVo vo, int page, int rows) {
        PageRequest pr =new PageRequest();
        List<Criteria> cts = new ArrayList<Criteria>();
        if (vo.getIsStart()==null) {
            cts.add(new Criteria(Customer("baseCompany", vo)));
            cts.add(new Criteria(Customer("destCompany", vo)));
        }else{
            cts.add(new Criteria(Customer(vo.getIsStart()==1?"baseCompany":"destCompany", vo)));
        }
        Filter filter = new Filter(cts);
        pr.setFilter(filter);
        pr.setPage(page);
        pr.setSize(rows);
        Sort sort= new Sort();
        sort.add(new Sort.Order(Sort.Direction.DESC,"ordertime"));
        pr.setSort(sort);
        return  tbCustomerOrderService.findAll(pr);
    }

    private List Customer(String company,CustomerOrderVo vo){
        List<Term> tr=new ArrayList<Term>();
        tr.add(new Term("flag","0"));//未删除
        tr.add(new Term(company+".id", vo.getBaseCompany()));
        if(vo.getBussinessCode()!=null&&(!vo.getBussinessCode().equals("")))
            tr.add(new Term("bussinessCode",Operator.LK,"%"+vo.getBussinessCode()+"%"));//运单号
        if(vo.getPolName()!=null&&(!vo.getPolName().equals("")))
            tr.add(new Term("polName",Operator.LK,"%"+vo.getPolName()+"%"));//起始港
        if(vo.getDelName()!=null&&(!vo.getDelName().equals("")))
            tr.add(new Term("delName",Operator.LK,"%"+vo.getDelName()+"%"));//目的港
        if(vo.getShipName()!=null&&(!vo.getShipName().equals("")))
            tr.add(new Term("baseVoya.vessel",Operator.LK,"%"+vo.getShipName()+"%"));//船名
        if(vo.getVoyage()!=null&&(!vo.getVoyage().equals("")))
            tr.add(new Term("baseVoya.voyage",Operator.LK,"%"+vo.getVoyage()+"%"));//航次
        if (vo.getBelongCompany()!=null && !vo.getBelongCompany().equals("")){
            if(vo.getBelongCompany().equals("全部")){
                vo.setBelongCompany("");
            }
            tr.add(new Term("belongCompany", Operator.LK, "%"+vo.getBelongCompany() + "%"));
        }
        if(vo.getFleetStatus()!=null&&(!vo.getFleetStatus().equals(""))) {
            String statusFld=company.equals("baseCompany")?"pol_orderState":"del_orderState";
            tr.add(new Term(statusFld, vo.getFleetStatus()));
        }

        //箱子的条件
        List<Term> ctnTrList=new ArrayList<Term>();
        if(vo.getShipStuats()!=null&&(!vo.getShipStuats().equals(""))) {
            ctnTrList.add(new Term("shipStatus", Operator.LK, vo.getShipStuats() + "%"));
        }
//        if(vo.getFleetStatus()!=null&&(!vo.getFleetStatus().equals(""))) {
//            List fsList = new ArrayList<>();
//            if (vo.getFleetStatus().equals("未派车")) {
//                fsList.add("未派车");
//            } else if (vo.getFleetStatus().equals("已执行")) {
//                fsList.add("已派车");
//                fsList.add("已提箱");
//                fsList.add("已装箱");
//                fsList.add("已拆箱");
//            } else {
//                fsList.add("已完成");
//            }
//            ctnTrList.add(new Term("fleetStatus", Operator.IN, fsList));
//        }
        if(vo.getContainerNo()!=null&&(!vo.getContainerNo().equals(""))) {
            ctnTrList.add(new Term("containerNo", Operator.LK, vo.getContainerNo() + "%"));
        }
        if (ctnTrList.size()>0){
            Filter filter =new Filter(new Criteria(ctnTrList));
            List<TbContainerInfo> flList=tbContainerInfoService.list(filter,null);
            List coIdList= new ArrayList<>();
            for(TbContainerInfo co:flList) {
                coIdList.add(co.getTbCustomerOrder().getId());
            }
            tr.add(new Term("id", Operator.IN, coIdList));      //航次状态
        }
        //车队名称模糊条件
        if(vo.getFleetName()!=null&&(!vo.getFleetName().equals(""))){
            List<Term> tr1 = new ArrayList<Term>();
            tr1.add(new Term("fleetCompany.companyName",Operator.LK,"%"+vo.getFleetName()+"%"));
            Filter filter =new Filter(new Criteria(tr1));
            List<TbFleetOrder> flList=tbFleetOrderService.list(filter,null);
            List coIdList= new ArrayList<>();
            for(TbFleetOrder co:flList) {
                coIdList.add(co.getTbCustomerOrder().getId());
            }
            tr.add(new Term("id", Operator.IN, coIdList));      //车队
        }

        return tr;
    }
    @Override
    public Page<TbContainerInfo> containerList(TbContainerInfoVo vo, int page, int rows) {
        PageRequest pr =new PageRequest();
        List<Term> tr=new ArrayList<Term>();
        tr.add(new Term("flag","0"));//未删除
        tr.add(new Term("tbCustomerOrder.id", vo.getCustomerOrder()));
        tr.add(new Term("isStart", vo.getIsStart()));
        Filter filter = new Filter(new Criteria( tr));
        pr.setFilter(filter);
        pr.setPage(page);
        pr.setSize(rows);
        return  tbContainerInfoService.findAll(pr);
    }

    @Override
    public List companyList(String companyId) {
        BaseCompanyOffice baseCompanyOffice = new BaseCompanyOffice();
        BaseCompany baseCompany = new BaseCompany();
        baseCompany.setId(companyId);
        baseCompanyOffice.setBaseCompany(baseCompany);
        return baseCompanyOfficeService.list(baseCompanyOffice,null);
    }


}
